﻿using System.Collections.Generic;
using StockLib;
using StockLib.Data;
using System.Linq;
using System.Windows.Forms;
using StockUtil;
using System;

namespace StockUI
{
    public partial class FormGraph : FormAbstractChild
    {
        public FormGraph()
        {
            InitializeComponent();

            StockContainer.Instance.DataChanged += new StockContainerDataEventHandler(StockContainer_DataChanged);
        }

        private void StockContainer_DataChanged(object sender, System.EventArgs e)
        {
            UpdateLatestKData(StockContainer.Instance.CurrentStock, stockGraph.KData);
        }

        protected override void ProcessNotifyMessage(FormNotifyMessage msg)
        {
            switch (msg.Message)
            {
                case "Graph.Show":
                    Stock stock = msg.Param1 as Stock;
                    if (stock != null && stock != StockContainer.Instance.CurrentStock)
                    {
                        //更改当前股票
                        StockContainer.Instance.CurrentStock = stock;

                        //绑定数据
                        BindGraphData(stock);

                        //更改图形窗口标题                        
                        this.Text = string.Format("{0} {1}", stock.Name, stock.Code);
                    }
                    stockGraph.Focus();
                    break;
            }
        }

        private void FormGraph_Load(object sender, System.EventArgs e)
        {
            this.ActiveControl = stockGraph;
        }

        private void BindGraphData(Stock stock)
        {
            List<KData> kdataDay = StockData.Instance.LoadKData(stock, KType.Day);
            UpdateLatestKData(stock, kdataDay);
            stockGraph.KData = kdataDay;
            stockGraph.Stock = stock;

            List<KData> kdataMinute = StockData.Instance.LoadKData(stock, KType.Min, 50);
            stockPrice.KData = kdataMinute;
            stockPrice.stock = stock;
        }

        private void UpdateLatestKData(Stock stock, List<KData> kdata)
        {
            if (kdata == null || stock == null)
                return;

            if (stock.Open > 0 && stock.Close > 0 && stock.New > 0)
            {
                if (kdata.Count <= 0 || (kdata.Count > 0 && (kdata[kdata.Count - 1].Time + 24 * 60 * 60) <= stock.Time))
                {
                    KData data = new KData();
                    data.Market = stock.Market;
                    data.Code = stock.Code;
                    data.Time = stock.Time;
                    data.Open = stock.Open;
                    data.Close = stock.New;
                    data.High = stock.High;
                    data.Low = stock.Low;
                    data.Volume = stock.Volume;
                    data.Amount = stock.Amount;

                    kdata.Add(data);
                }
                else
                {
                    kdata[kdata.Count - 1].Time = stock.Time;
                    kdata[kdata.Count - 1].Open = stock.Open;
                    kdata[kdata.Count - 1].Close = stock.New;
                    kdata[kdata.Count - 1].High = stock.High;
                    kdata[kdata.Count - 1].Low = stock.Low;
                    kdata[kdata.Count - 1].Volume = stock.Volume;
                    kdata[kdata.Count - 1].Amount = stock.Amount;
                }
            }
        }

        private void FormGraph_Enter(object sender, System.EventArgs e)
        {
            stockGraph.Focus();
        }

        private void cmdBack_Click(object sender, System.EventArgs e)
        {
            SendKeys.Send("{ESC}");
        }

        private void cmdF10_Click(object sender, System.EventArgs e)
        {
            SendKeys.Send("{F10}");
        }

        private void cmdBlock_Click(object sender, System.EventArgs e)
        {
            SendKeys.Send("^r");
        }

        private void cmdBlockPut_Click(object sender, System.EventArgs e)
        {
            SendKeys.Send("^z");
        }

        private void cmdBlockZXG_Click(object sender, System.EventArgs e)
        {
            SendKeys.Send("%z");
        }

        private void cmdBlockDelete_Click(object sender, System.EventArgs e)
        {
            SendKeys.Send("{DELETE}");
        }

        private void cmdMainIndicatorShow_Click(object sender, System.EventArgs e)
        {
            SendKeys.Send("{TAB}");
        }

        private void cmdKLineType_Click(object sender, System.EventArgs e)
        {
            KLineType klineType = (KLineType)Convert.ToInt32((sender as ToolStripItem).Tag);
            stockGraph.KLineType = klineType;
        }

        private void cmdKLineType_DropDownOpened(object sender, EventArgs e)
        {
            string klineType = Convert.ToString(Convert.ToInt32(stockGraph.KLineType));
            cmdKLineType1.Checked = klineType.Equals(cmdKLineType1.Tag);
            cmdKLineType2.Checked = klineType.Equals(cmdKLineType2.Tag);
            cmdKLineType3.Checked = klineType.Equals(cmdKLineType3.Tag);
            cmdKLineType4.Checked = klineType.Equals(cmdKLineType4.Tag);
        }
    }
}
